package cn.home.resident.controller;

import java.util.*;
import javax.servlet.http.HttpServletResponse;

import cn.home.contract.domain.HomeContractInfo;
import cn.home.resident.domain.HomeUserHealthtag;
import cn.home.resident.mapper.HomeUserHealthtagMapper;
import cn.home.resident.mapper.HomeUserMapper;
import cn.home.resident.service.HomeUserHealthtageService;
import cn.home.service.domain.HomeService;
import cn.home.service.domain.HomeServiceRecord;
import cn.home.sysmanagement.domain.HomeTag;
import cn.home.sysmanagement.service.IHomeTagService;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import cn.home.common.annotation.Log;
import cn.home.common.core.controller.BaseController;
import cn.home.common.core.domain.AjaxResult;
import cn.home.common.enums.BusinessType;
import cn.home.resident.domain.HomeUser;
import cn.home.resident.service.IHomeUserService;
import cn.home.common.utils.poi.ExcelUtil;
import cn.home.common.core.page.TableDataInfo;

/**
 * 居民Controller
 *
 * @author homedoctor
 * @date 2025-04-09
 */
@RestController
@RequestMapping("/resident/resident")
public class HomeUserController extends BaseController {
    @Autowired
    private IHomeTagService homeTagService;
    @Autowired
    private HomeUserHealthtageService homeUserHealthtageService;
    @Autowired
    private IHomeUserService homeUserService;
@Autowired
private  HomeUserHealthtagMapper homeUserHealthtagMapper;
@Autowired
private HomeUserMapper homeUserMapper;
    /**
     * 查询居民列表
     */
    @PreAuthorize("@ss.hasPermi('resident:resident:list')")
    @GetMapping("/list")
    public TableDataInfo list(HomeUser homeUser) {
        startPage();
        List<HomeUser> list = homeUserService.selectHomeUserList(homeUser);
        list.forEach(System.out::println);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('resident:resident:list')")
    @GetMapping("/residentlist")
    public TableDataInfo residentlist(HomeUser homeUser) {
        startPage();
        List<HomeUser> homeUsers = homeUserService.selectAllfirstpage();
        homeUsers.forEach(System.out::println);
        return getDataTable(homeUsers);
    }

    /**
     * 导出居民列表
     */
    @PreAuthorize("@ss.hasPermi('resident:resident:export')")
    @Log(title = "居民", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HomeUser homeUser) {
        List<HomeUser> list = homeUserService.selectHomeUserList(homeUser);
        ExcelUtil<HomeUser> util = new ExcelUtil<HomeUser>(HomeUser.class);
        util.exportExcel(response, list, "居民数据");
    }

    /**
     * 获取居民详细信息
     */
    @PreAuthorize("@ss.hasPermi('resident:resident:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(homeUserService.selectHomeUserById(id));
    }

    @PreAuthorize("@ss.hasPermi('resident:resident:query')")
    @GetMapping(value = "/getInfodetail")
    public AjaxResult getInfodetail( Integer id) {
        return success(homeUserService.selectHomeUserByIddetail(id));
    }

    /**
     * 新增居民
     */
//    @PreAuthorize("@ss.hasPermi('resident:resident:add')")
    @Log(title = "居民", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HomeUser homeUser) {

        return toAjax(homeUserService.insertHomeUser(homeUser));
    }
    //    @PreAuthorize("@ss.hasPermi('resident:resident:add')")
//    @Log(title = "居民", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult aadd(@RequestBody HomeUser homeUser) {

        return toAjax(homeUserService.insertHomeUser(homeUser));
    }

    /**
     * 修改居民
     */
    @PreAuthorize("@ss.hasPermi('resident:resident:edit')")
    @Log(title = "居民", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HomeUser homeUser) {
        return toAjax(homeUserService.updateHomeUser(homeUser));
    }

    /**
     * 删除居民
     */
    @PreAuthorize("@ss.hasPermi('resident:resident:remove')")
    @Log(title = "居民", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(homeUserService.deleteHomeUserByIds(ids));
    }

    @PreAuthorize("@ss.hasPermi('resident:resident:list')")
    @GetMapping("/getlable")
    public AjaxResult getlable() {
        System.out.print("---------------------->" + homeTagService.selectHomeTagList(new HomeTag()));
        return AjaxResult.success(homeTagService.selectHomeTagList(new HomeTag()));
    }

    @PreAuthorize("@ss.hasPermi('resident:resident:list')")
    @PostMapping("/settag")
    public AjaxResult settag(@RequestBody List<HomeUserHealthtag> tags) {
        tags.forEach(item ->
                {
                    homeUserHealthtageService.insert(item);
                }
        );

        return AjaxResult.success("标签插入成功");
    }



    @PreAuthorize("@ss.hasPermi('resident:resident:list')")
    @GetMapping("/deletetags")
    public AjaxResult deletetags(Long userid) {
        int i = homeUserHealthtagMapper.deleteByPrimaryKey(userid);
        return AjaxResult.success("标签删除成功"+i);
    }



    @PreAuthorize("@ss.hasPermi('resident:resident:list')")
    @PostMapping("/addtags")
    public AjaxResult addtags(@RequestBody Map<String,Object> map) {
//        String useridStr = (String)map.get("userid");
//        Integer userid = Integer.valueOf(useridStr);
        Integer userid = (Integer) map.get("userid");
        List<Integer> list = (List<Integer>)map.get("list");
        for (Integer i : list) {
            HomeUserHealthtag homeUserHealthtag = new HomeUserHealthtag(userid,i);
            int insert = homeUserHealthtageService.insert(homeUserHealthtag);

        }
        return AjaxResult.success("标签插入成功");
    }
    @PreAuthorize("@ss.hasPermi('resident:resident:list')")
    @GetMapping("/qianyue")
    public AjaxResult qianyue(Integer id) {
        HomeContractInfo homeContractInfo = homeUserMapper.selectHomeContractInfoByIddetail(id);
        return success(homeContractInfo);
    }
    @PreAuthorize("@ss.hasPermi('resident:resident:list')")
    @GetMapping("/contractlist")
    public AjaxResult contractlist(Integer id) {
        startPage();
        List<HomeContractInfo> selecthomecontractinfobyid = homeUserMapper.selecthomecontractinfobyid(id);
        return success(selecthomecontractinfobyid);
    }

    @PreAuthorize("@ss.hasPermi('resident:resident:list')")
    @GetMapping("/homeservicelist")
    public AjaxResult homeservicelist(Integer id) {

        List<HomeServiceRecord> homeServices = homeUserMapper.selectHomeServicebyid(id);
        return success(homeServices);
    }


    @PreAuthorize("@ss.hasPermi('resident:resident:list')")
    @GetMapping("/getdataservice")
    public AjaxResult getdataservice(@RequestParam("start") @DateTimeFormat(pattern = "YYYY-MM-DD")  Date start,@RequestParam("end") @DateTimeFormat(pattern ="YYYY-MM-DD")  Date end) {
        System.out.println(start);
        System.out.println(end);
        List<Map<String, Object>> maps = homeUserMapper.selectServicePackagebytime(start,end);
        return success(maps);
    }

    @PreAuthorize("@ss.hasPermi('resident:resident:list')")
    @GetMapping("/getdataserviceitem")
    public AjaxResult getdataserviceitem(@RequestParam("start") @DateTimeFormat(pattern = "YYYY-MM-DD")  Date start,@RequestParam("end") @DateTimeFormat(pattern ="YYYY-MM-DD")  Date end) {
        System.out.println(start);
        System.out.println(end);
        List<Map<String, Object>> maps = homeUserMapper.selectserviceitembytime(start,end);
        return success(maps);
    }

    @PreAuthorize("@ss.hasPermi('resident:resident:list')")
    @GetMapping("/getresidentage")
    public AjaxResult getresidentage() {

        List<Map<Object, Object>> selectresidentage = homeUserMapper.selectresidentage();
        return success(selectresidentage);
    }
    @PreAuthorize("@ss.hasPermi('resident:resident:list')")
    @GetMapping("/getresidentculture")
    public AjaxResult getresidentculture() {

        List<Map<Object, Object>> selectresidentculture = homeUserMapper.selectresidentculture();
        return success(selectresidentculture);
    }

    @PreAuthorize("@ss.hasPermi('resident:resident:list')")
    @GetMapping("/getresidentsex")
    public AjaxResult getresidentsex() {

        List<Map<Object, Object>> selectresidentsex = homeUserMapper.selectresidentsex();
        return success(selectresidentsex);
    }


    @PreAuthorize("@ss.hasPermi('resident:resident:list')")
    @GetMapping("/getresidenttag")
    public AjaxResult getresidenttag() {

        List<Map<Object, Object>> selectresidenttag = homeUserMapper.selectresidenttag();
        return success(selectresidenttag);
    }
    @PreAuthorize("@ss.hasPermi('resident:resident:list')")
    @GetMapping("/getresidentservicepackageuse")
    public AjaxResult getresidentservicepackageuse() {

        List<Map<Object, Object>> selectservicepackageuse = homeUserMapper.selectservicepackageuse();

        return success(selectservicepackageuse);
    }


}
